package com.jwdf.uda.ui.adapter.viewholder;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.view.View;

import com.jwdf.uda.R;
import com.jwdf.uda.bean.PbBean;
import com.jwdf.uda.databinding.ItemQuizConcernBinding;
import com.jwdf.uda.event.QuizDetailEvent;
import com.jwdf.uda.ui.activity.UserInfoActivity;
import com.jwdf.uda.ui.adapter.AbsViewHolder;
import com.jwdf.uda.ui.adapter.item.QuizDetailItem;
import com.jwdf.uda.utils.DateUtils;
import com.jwdf.uda.utils.EventBusUtil;
import com.jwdf.uda.utils.StringUtils;
import com.nostra13.universalimageloader.core.ImageLoader;
/**
 * Created by Administrator on 2016/9/6.
 */
public class QuizConcernViewHolder extends AbsViewHolder<QuizDetailItem> {
    private ItemQuizConcernBinding mItemBinding;

    public QuizConcernViewHolder(View convertView) {
        super(convertView);
        mItemBinding = DataBindingUtil.bind(convertView);
        mItemBinding.ivHeader.setOnClickListener(this);
        mItemBinding.tvName.setOnClickListener(this);
    }

    @Override
    protected void onBindView(final QuizDetailItem item) {
        PbBean.PbUserSimple userInfo = item.userInfo;
        if(userInfo == null) return;
        ImageLoader.getInstance().displayImage(userInfo.getAvatar().getUrl(), mItemBinding.ivHeader);
        mItemBinding.tvName.setText(StringUtils.filterNullAndTrim(userInfo.getNickname()));
        mItemBinding.setIsDesigner(userInfo.getIsDesigner());
        final Context context = convertView.getContext();
        mItemBinding.tvCreateTime.setText(DateUtils.getFriendlyTime(item.createTime));
        mItemBinding.tvConcernCount.setText(convertView.getContext().getString(R.string.concern_count, ""+item.concernCount));
        mItemBinding.tvConcernQuiz.setChecked(item.isConcern);
        mItemBinding.tvConcernQuiz.setText(item.isConcern ? context.getString(R.string.user_cancel_follow) : context.getString(R.string.user_follow_quiz));
        mItemBinding.tvConcernQuiz.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                QuizDetailEvent event = new QuizDetailEvent(item.isConcern ? QuizDetailEvent.EVENT_CANCEL_CONCERN : QuizDetailEvent.EVENT_ADD_CONCERN);
                event.count = item.concernCount;
                EventBusUtil.post(event);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_header:
            case R.id.tvName:
                UserInfoActivity.start(v.getContext(), item.userInfo.getUserId(), item.userInfo.getIsDesigner());
                break;
        }
    }

}
